package com.example.lottery.controller;

import com.example.lottery.common.errorcode.ControllerErrorCodeConstants;
import com.example.lottery.common.exception.ControllerException;
import com.example.lottery.common.pojo.CommonResult;
import com.example.lottery.controller.param.CreatePrizeParam;
import com.example.lottery.controller.param.FindPrizeListParam;
import com.example.lottery.controller.result.FindPrizeListResult;
import com.example.lottery.controller.result.PrizeCreateResult;
import com.example.lottery.service.IPictureService;
import com.example.lottery.service.IPrizeService;
import com.example.lottery.service.dto.FindPrizeListDTO;
import com.example.lottery.service.dto.PrizeCreateDTO;
import com.example.lottery.service.dto.PrizeDTO;
import com.example.lottery.service.impl.PictureServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
@RestController
@RequestMapping("/prize")
public class PrizeController {
    @Autowired
    private IPrizeService prizeService;
    @PostMapping(value = "/create")
    public CommonResult<PrizeCreateResult> createPrize(@Validated @RequestPart("param") CreatePrizeParam param,
    @RequestPart("prizePic") MultipartFile prizePic){
        PrizeCreateDTO prize = prizeService.createPrize(param, prizePic);
        return CommonResult.success(convertToPrizeCreateResult(prize));
    }

    private PrizeCreateResult convertToPrizeCreateResult(PrizeCreateDTO prize) {
        if(prize==null){
            log.warn(ControllerErrorCodeConstants.PRIZE_CREATE_ERROR.getMsg());
            throw new ControllerException(ControllerErrorCodeConstants.PRIZE_CREATE_ERROR);
        }
        PrizeCreateResult prizeCreateResult=new PrizeCreateResult();
        prizeCreateResult.setId(prize.getId());
        return prizeCreateResult;
    }

    @GetMapping("/find-list")
    public CommonResult<FindPrizeListResult> findPrizeList(@Validated FindPrizeListParam findPrizeListParam){
        FindPrizeListDTO<PrizeDTO> findPrizeListDTO=prizeService.findPrizeList(findPrizeListParam);
        return CommonResult.success(convertToPrizeFindPrizeListResult(findPrizeListDTO));
    }

    private FindPrizeListResult convertToPrizeFindPrizeListResult(FindPrizeListDTO<PrizeDTO> findPrizeListDTO) {
        if(findPrizeListDTO==null){
            log.warn(ControllerErrorCodeConstants.PRIZE_FIND_LIST_ERROR.getMsg());
            throw new ControllerException(ControllerErrorCodeConstants.PRIZE_FIND_LIST_ERROR);
        }
        FindPrizeListResult findPrizeListResult=new FindPrizeListResult();
        findPrizeListResult.setTotal(findPrizeListDTO.getTotal());
        findPrizeListResult.setRecords(findPrizeListDTO.getRecords().stream()
                .map(
                        prizeDTO -> {
                            FindPrizeListResult.PrizeInfo prizeInfo = new FindPrizeListResult.PrizeInfo();
                            prizeInfo.setPrizeId(prizeDTO.getId());
                            prizeInfo.setPrizeName(prizeDTO.getName());
                            prizeInfo.setDescription(prizeDTO.getDescription());
                            prizeInfo.setImageUrl(prizeDTO.getImageUrl());
                            prizeInfo.setPrice(prizeDTO.getPrice());
                            return prizeInfo;
                        }
                ).collect(Collectors.toList()));
        return findPrizeListResult;

    }
}
